<?php
/**
 * CsvExporterActiveDataProvider
 *
 * PHP version 5
 *
 * @category
 * @package
 * @author   Evgeny Utehin <utehin@ebola.com.ua>
 */

class CsvExporterActiveDataProvider extends CsvExporterWorker
{
    protected function getReportHeaders()
    {
        $header = array();

        if (!$this->_onlyIncluded) {
            foreach ($this->_dataProvider->model->attributes as $attr => $value) {
                if (!in_array($attr, $this->_excludedColumns)) {
                    array_push($header, $this->getHeader($attr));
                }
            }
        }

        foreach ($this->_includedColumns as $key => $value) {
            if (is_array($value)) {
                array_push($header, $this->getHeader($key, $value));
            } else {
                array_push($header, $this->getHeader($value));
            }
        }

        return $header;
    }

    protected function getHeader(&$column, $details = array())
    {
        if (isset($details['relation'])) {
            $modelRelation = $this->_dataProvider->model->getActiveRelation($details['relation']);

            if ($modelRelation instanceof CBelongsToRelation || $modelRelation instanceof CHasOneRelation) {
                if (isset($details['header'])) {
                    return $details['header'];
                } else {
                    $relatedModel = new $modelRelation->className;

                    return $relatedModel->getAttributeLabel($column);
                }
            } else {
                throw new Exception(get_class($this) . ": Relation <{$details['relation']}>"
                    . " must be an instance of CBelongsToRelation or CHasOneRelation");
            }
        } else {
            if (isset($details['header'])) {
                return $details['header'];
            } else {
                return $this->_dataProvider->model->getAttributeLabel($column);
            }
        }
    }

    protected function getReportBody($fp)
    {
        $rowData = array();

        if ($this->_onlyIncluded) {
            foreach ($this->_data as $row) {
                $this->getIncludedColumnsData($rowData, $row);

                fputcsv($fp, $rowData, $this->delimiter, $this->enclosure);
                $rowData = array();
            }
        } else {
            foreach ($this->_data as $row) {
                foreach ($this->_dataProvider->model->attributes as $attr => $value) {
                    if (!in_array($attr, $this->_excludedColumns)) {
                        array_push($rowData, $this->getValue($row, $attr));
                    }
                }

                $this->getIncludedColumnsData($rowData, $row);

                fputcsv($fp, $rowData, $this->delimiter, $this->enclosure);
                $rowData = array();
            }
        }
    }

    protected function getIncludedColumnsData(&$rowData, $row)
    {
        foreach ($this->_includedColumns as $key => $value) {
            if (is_array($value)) {
                array_push($rowData, $this->getValue($row, $key, $value));
            } else {
                array_push($rowData, $this->getValue($row, $value));
            }
        }
    }

    protected function getValue($object, $column, $details = array())
    {
        if (empty($details) || !isset($details['relation'])) {
            return $object->$column;
        } else {
            if (isset($details['relation'])) {
                $relation = $object->$details['relation'];

                return $relation[$column];
            }
        }
    }
}
